package xc.com.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回格式
 *
 * @author 周利忠
 * @version 1.0
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -3147637147275662202L;
    /**
     * 是否成功
     */
    private boolean flag;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功!";
    }

    /**
     * 用于添加、修改类方法返回数据
     *
     * @param b              是否成功
     * @param successMessage 成功返回消息
     * @param failMessage    失败返回消息
     */
    public Result(boolean b, String successMessage, String failMessage) {
        this.flag = b;
        this.code = b ? StatusCode.OK : StatusCode.FAIL;
        this.message = b ? successMessage : failMessage;
    }

    public Result(Integer i, String successMessage, String failMessage) {
        this(i > 0, successMessage, failMessage);
    }
}
